建立商品的ViewModel
public class CartView
{
public List<CartBuy> DataList { get; set; }
public bool isCartsave { get; set; }
}
建立購物車功能模組
public class CartService
{
private ShoppingEntities db = new ShoppingEntities();
public List<CartBuy> GetItemFromCart(string Cart)
{
return db.CartBuy.Where(p => p.Cart_Id == Cart).ToList();
}
public bool CheckInCart(string Cart, int Item_Id)
{
CartBuy SearchData = db.CartBuy
.Where(p => p.Cart_Id == Cart && p.Item_Id == Item_Id)
.FirstOrDefault();
return (SearchData != null);
}
public bool CheckCartSave(string user, string Cart)
{
CartSave SearchData = db.CartSave
.Where(p => p.Account == user && p.Cart_Id == Cart)
.FirstOrDefault();
return (SearchData != null);
}
public string GetCartSave(string user)
{
CartSave SearchData = db.CartSave
.Where(p => p.Account == user).FirstOrDefault();
if (SearchData != null)
{
return SearchData.Cart_Id;
}
else
{
return null;
}
}
public void AddtoCart(string Cart, int Item_Id)
{
CartBuy newData = new CartBuy();
newData.Cart_Id = Cart;
newData.Item_Id = Item_Id;
db.CartBuy.Add(newData);
db.SaveChanges();
}
public void RemoveForCart(string Cart, int Item_Id)
{
CartBuy DeleteData = db.CartBuy
.Where(p => p.Cart_Id == Cart && p.Item_Id == Item_Id)
.FirstOrDefault();
db.CartBuy.Remove(DeleteData);
db.SaveChanges();
}
public void SaveCart(string user, string Cart)
{
CartSave newData = new CartSave();
newData.Account = user;
newData.Cart_Id = Cart;
db.CartSave.Add(newData);
db.SaveChanges();
}
public void SaveCartRemove(string user)
{
CartSave DeleteData = db.CartSave
.Where(p => p.Account == user).FirstOrDefault();
db.CartSave.Remove(DeleteData);
db.SaveChanges();
}
}